<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Command;

use App\GrpcClient\HiClient;
use App\GrpcClient\UserClient;
use Grpc\HiUser;
use Hyperf\Command\Annotation\Command;
use Hyperf\Command\Command as HyperfCommand;
use Psr\Container\ContainerInterface;
use User\GetUserInfoRequest;

/**
 * @Command
 */
class Client extends HyperfCommand
{
    /**
     * @var ContainerInterface
     */
    protected $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;

        parent::__construct('grpc:client');
    }

    public function configure()
    {
        parent::configure();
        $this->setDescription('Hyperf Demo Command');
    }

    public function handle()
    {
        $this->getUserInfo();
        return 0;
    }

    protected function getUserInfo()
    {
        $client = new UserClient('127.0.0.1:9888', [
            'credentials' => null,
        ]);

        $request = new GetUserInfoRequest();
        $request->setUserId(1);
        [$reply, $status] = $client->getUserInfo($request);
        var_dump($status);
        var_dump($reply->serializeToJsonString());
    }

    protected function sayHello()
    {
        $client = new HiClient('127.0.0.1:9888', [
            'credentials' => null,
        ]);
        $request = new HiUser();
        $request->setName('wangzhihui');
        $request->setSex(1);
        [$reply, $status] = $client->sayHello($request);
        var_dump($status);
        $message = $reply->getMessage();
        /** @var HiUser $user */
        $user = $reply->getUser();
        var_dump($message, $user->getName());
    }
}
